package com.dk.controller;

import com.dk.pojo.Clazz;
import com.dk.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping("test")
    @ResponseBody
    public String test(){
        System.out.println("大嘎好,我系古天乐");
        return "OK";
    }
    //将视图解析器封装到了模板引擎里面,封装到了ThymeleafProperties中
    @RequestMapping("test1")
    public String test1(){
        return "index";
    }
    @RequestMapping("toshow")
    public String  toShow(Model model){
//        String username = "古天乐";
        List<User> userlist = new ArrayList<>();
        User u = new User(18,"古天乐","男",new Date(),"来砍我");
        User u1 = new User(19,"古巨基","男",new Date(),"基基");
        User u2 = new User(20,"吴亦凡","女",new Date(),"大黄");
        userlist.add(u);
        userlist.add(u1);
        userlist.add(u2);
//        model.addAttribute("user",username);
        model.addAttribute("userlist",userlist);
        model.addAttribute("value","<h1>搭嘎好</h1>");
        return "user/show";
    }
    @RequestMapping("toUpdate")
    public String toUpdate(Integer id , Model model, HttpServletRequest request){
        User u = new User(id,"古天乐","男",new Date(),"来砍我",2);
        //查询所有的班级
        Clazz c1 = new Clazz(1,"2102");
        Clazz c2 = new Clazz(2,"2103");
        Clazz c3 = new Clazz(3,"2104");
        List<Clazz> ClazzList = new ArrayList<>();
        ClazzList.add(c1);
        ClazzList.add(c2);
        ClazzList.add(c3);
        model.addAttribute("ClazzList",ClazzList);
        model.addAttribute("u",u);
        request.getSession().setAttribute("sessionu",u);
        return "user/update";
    }
    @RequestMapping("delete/{id}")
    @ResponseBody
    public String todelete(@PathVariable("id")Integer id){
        System.out.println(id);
        return "OK";
    }

}
